//
//  FullScreenViewController.m
//  SelfDriving
//
//  Created by apple on 16/9/1.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "FullScreenViewController.h"
#import <AVFoundation/AVFoundation.h>

@interface FullScreenViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *bgimg;

@end

@implementation FullScreenViewController
{
    AVPlayer *_player;
    AVPlayerItem *_playItem;
    AVPlayerLayer *_playerLayer;
    AVPlayerLayer *_fullPlayer;
    UIButton *playbtn;
}
- (void)viewDidLoad {
    self.navigationController.navigationBarHidden = YES;
    [super viewDidLoad];
    //    _bgimg.image = [UIImage imageNamed:@""];
    if (_isimg) {
        [ValidateHelper setImage:[_url absoluteString] img:_bgimg istoux:NO];
    }else{
        _playItem = [AVPlayerItem playerItemWithURL:_url];
        _player = [AVPlayer playerWithPlayerItem:_playItem];
        _playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
        _playerLayer.frame = self.view.frame;
        _playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;//视频填充模式
        [_bgimg.layer addSublayer:_playerLayer];
        
        playbtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, SCRE_WIDTH, SCRE_HEIGHT)];
        [playbtn addTarget:self action:@selector(playaction:) forControlEvents:UIControlEventTouchUpInside];
        [playbtn setImage:[UIImage imageNamed:@"图标---视频播放"] forState:UIControlStateNormal];
        
        [self play];
    }
    self.view.backgroundColor = [UIColor clearColor];
    UIButton *deleteBtn = [[UIButton alloc]initWithFrame:CGRectMake(SCRE_WIDTH - 70, 20, 70, 45)];
    [deleteBtn setTitle:@"X" forState:UIControlStateNormal];
    [deleteBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [deleteBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:deleteBtn];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinished1:)name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
    
    
    
}
-(void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
-(void)play{
    playbtn.hidden = YES;
    [_player play];
}
-(void)playbackFinished1:(NSNotification *)notification
{
    playbtn.hidden = NO;
}
-(void)playaction:(UIButton *)btn{
    [self play];
}
-(void)backAction:(UIButton *)btn{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)viewWillDisappear:(BOOL)animated{
    self.navigationController.navigationBarHidden = NO;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
